# 编译器及参数设定
# ---------------------------------------------------------------------------
TARGET         := app_demo.out

CC             := gcc
CFLAGS         := -Wall -Wextra -MMD -MP

CXX            := g++
CXXFLAGS       := -Wall -Wextra -std=c++11 -MMD -MP

LDFLAGS        := 

SRCDIR         := .
C_SRCS         := $(wildcard $(SRCDIR)/*.c)
CPP_SRCS       := $(wildcard $(SRCDIR)/*.cpp)

OBJS           := $(C_SRCS:$(SRCDIR)/%.c=%.o) $(CPP_SRCS:$(SRCDIR)/%.cpp=%.o)
DEPS           := $(OBJS:.o=.d)

CLEAN_FILES    += $(TARGET) $(OBJS) $(DEPS)

# 输出模式控制
# ---------------------------------------------------------------------------
ifneq ($(V),1)
quiet = quiet_
Q = @
else
quiet =
Q =
endif


# 命令定义 (格式: quiet_cmd_<类型> + cmd_<类型>)
# ---------------------------------------------------------------------------
quiet_cmd_cxx   = CXX     $<
      cmd_cxx   = $(CXX) $(CXXFLAGS) -c $< -o $@

quiet_cmd_cc    = CC      $<
      cmd_cc    = $(CC) $(CFLAGS) -c $< -o $@

quiet_cmd_ld    = LD      $@
      cmd_ld    = $(CXX) $(LDFLAGS) $^ -o $@

quiet_cmd_clean = CLEAN   $(CLEAN_FILES)
      cmd_clean = rm -f $(CLEAN_FILES)

# 通用命令执行函数 用法: $(call cmd,<命令类型>,<显示参数>)
# ---------------------------------------------------------------------------
cmd = $(Q)$(if $($(quiet)cmd_$(1)),\
    @printf "  %-8s%s\n" "$($(quiet)cmd_$(1))"; $(cmd_$(1)),\
    $(cmd_$(1)))

# 构建规则
# ---------------------------------------------------------------------------
all: $(TARGET)

$(TARGET): $(OBJS)
	$(call cmd,ld)

%.o: %.c
	$(call cmd,cc,$<)

%.o: %.cpp
	$(call cmd,cxx,$<)

# 包含自动生成的依赖
# ---------------------------------------------------------------------------
-include $(DEPS)

# clean
# ---------------------------------------------------------------------------
clean:
	$(call cmd,clean)

.PHONY: all clean
